//
//  ConDrawCountViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/4/5.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConDrawCountViewController.h"
#import "VefityTool.h"
#import "MBProgressHUD+MJ.h"
#import "SZLAccountHelper.h"
#import "MyCountNetwork.h"
#import "WaterRippleWait.h"
#import "ConDrawDetailViewController.h"

@interface ConDrawCountViewController ()

@property (weak, nonatomic) IBOutlet UILabel *typeNameLab;
@property (weak, nonatomic) IBOutlet UILabel *accountLab;
@property (weak, nonatomic) IBOutlet UITextField *countField;
@property (weak, nonatomic) IBOutlet UILabel *leftMoneyLab;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation ConDrawCountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现管理";
    self.view.backgroundColor = RGBColor(239, 239, 244);
    self.typeNameLab.text = self.listModel.typeName;
    self.accountLab.text = self.listModel.withdrawNumber;
    self.leftMoneyLab.text = [NSString stringWithFormat:@"可用余额%.2f元",self.countModel.balance];
    [self.countField becomeFirstResponder];
}
- (IBAction)btnClick:(UIButton *)btn
{
    [self.view endEditing:YES];
    if (btn.tag == 0) {
        self.countField.text = [NSString stringWithFormat:@"%.2f",self.countModel.balance];
    }else{
        if (![VefityTool isValidNumber:self.countField.text]) {
            [MBProgressHUD showError:@"请输入有效的提现金额" toView:self.view];
            return;
        }
        if ([self.countField.text floatValue]>self.countModel.balance) {
            [MBProgressHUD showError:@"提现金额不能大于余额" toView:self.view];
            return;
        }
        NSMutableDictionary *parame = [NSMutableDictionary dictionary];
        parame[@"token"] = [SZLAccountHelper shareInstance].token;
        parame[@"accountId"] = @(self.listModel.ID);
        parame[@"userAmount"] = [NSString stringWithFormat:@"%@",self.countField.text];
        [self showWaiter];
        [MyCountNetwork withDrawWithParame:parame response:^(BOOL success, NSString *msg,NSInteger ID) {
            [self hideWaiter];
            if (success) {
                [MBProgressHUD showSuccess:@"提交申请成功" toView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    ConDrawDetailViewController *detailVc = [[ConDrawDetailViewController alloc]init];
                    detailVc.ID = ID;
                    detailVc.count = self.countField.text;
                    [self.navigationController pushViewController:detailVc animated:YES];
                });
            }else{
                [MBProgressHUD showError:msg toView:self.view];
            }
        }];
    }
}
- (IBAction)textFieldDidChange:(UITextField *)field
{
    if (field.text.length>0) {
        self.sureBtn.backgroundColor = [UIColor blueColor];
    }else {
        self.sureBtn.backgroundColor = [UIColor lightGrayColor];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
